// licenseServerBatchModelDlg.cpp : implementation file
//

#include "stdafx.h"
#include "licenseServerBatchModel.h"
#include "licenseServerBatchModelDlg.h"

#include "reggen.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// ClicenseServerBatchModelDlg dialog




ClicenseServerBatchModelDlg::ClicenseServerBatchModelDlg(CWnd* pParent /*=NULL*/)
	: CDialog(ClicenseServerBatchModelDlg::IDD, pParent)
	, m_nMinValue(0)
	, m_nMaxValue(0)
	, m_nStep(0)
	, m_szTargetFile(_T("C:\\DCM_SNs.txt"))
	, m_nProductId(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void ClicenseServerBatchModelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_nMinValue);
	DDX_Text(pDX, IDC_EDIT2, m_nMaxValue);
	DDX_Text(pDX, IDC_EDIT3, m_nStep);
	DDX_Text(pDX, IDC_EDIT4, m_szTargetFile);
	DDX_Text(pDX, IDC_EDIT5, m_nProductId);
}

BEGIN_MESSAGE_MAP(ClicenseServerBatchModelDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_FILE, &ClicenseServerBatchModelDlg::OnBnClickedButtonFile)
	ON_BN_CLICKED(IDC_BUTTON_GEN, &ClicenseServerBatchModelDlg::OnBnClickedButtonGen)
END_MESSAGE_MAP()


// ClicenseServerBatchModelDlg message handlers

BOOL ClicenseServerBatchModelDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	m_nMinValue = 10000;
	m_nMaxValue = 11999;
	m_nStep = 1;
	m_nProductId = 1;

	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void ClicenseServerBatchModelDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR ClicenseServerBatchModelDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void ClicenseServerBatchModelDlg::OnBnClickedButtonFile()
{
	// TODO: Add your control notification handler code here
}

void ClicenseServerBatchModelDlg::OnBnClickedButtonGen()
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

    CStdioFile   file;   
    file.Open(m_szTargetFile, CFile::modeCreate|CFile::modeReadWrite|CFile::typeText);   

	DWORD uid = m_nProductId; 

	char szReg[_MAX_PATH];
	char szReg2[_MAX_PATH];
	TCHAR wszReg2[_MAX_PATH];
	TCHAR tszSN[_MAX_PATH];

	int nTimes = (  -  +1 ) / m_nStep;
	
	for ( int i=m_nMinValue; i<=m_nMaxValue; i+=m_nStep )
	{
		GenerateKey( i, uid, m_nProductId, (char*)&szReg );
		InfoToText( (char*)&szReg, uid, -1, (char*)&szReg2 );

#ifdef _UNICODE 
		swprintf( tszSN, _T("%8d:    "), i );
		file.WriteString( tszSN );

		int len = strlen( szReg2 );
		MultiByteToWideChar(CP_ACP, 0, szReg2, len, wszReg2, len );
	    file.WriteString( (TCHAR*)wszReg2);   

#else
		sprintf( tszSN, _T("%8d:    "), i );
		file.WriteString( tszSN );
	    file.WriteString( (char*)szReg2);   
#endif

		file.WriteString(_T("\n"));
		Sleep(0);
	}


    file.Close();   

	MessageBox(_T("Done!"));

}
